#include "list.h"
void SLprint(CA* pca)//打印链表，可以传空
{
    CA* cur = pca;
    while(cur)
    {
        printf("%d ->",cur->a);
        cur = cur->p;
    }
    printf("NULL\n");
    return ;
}

CA* Crea_node(SLdata X)//创建一个链表节点
{
    CA* newnode = (CA*)malloc(sizeof(CA));
    if(!newnode)
    {
        perror("malloc fail:");
        return NULL;
    }
    newnode->a = X;
    newnode->p = NULL;
    return newnode;
}
void SL_firinsert(CA** pca,SLdata data)//头插，这里传二级指针，是为了确实修改节点，
//如果直接传以及指针，是不是就无法修改你改变头节点之后的值的还回去
{
    assert(pca);
    CA* cur = *pca;
    CA* temp = Crea_node(data);
    temp ->a = data;
    temp->p = cur;
    *pca = temp;
    return;
}

void SL_endinsert(CA** pca,SLdata data)//尾插
//这里一样，如果是一开始传的空头，那么就需要二级指针，
{
    assert(pca);
    CA* temp = Crea_node(data);
    temp->a = data;
    temp->p=NULL;
    CA* p = *pca;
    if(p)
    {
        
        while(p->p)
        {
            p = p->p;
        }
        p->p = temp;
    }
    else
    {
        *pca = temp;
    }
    return ;
}




void SL_deletefir(CA** pca)
{
    assert(pca);
    if(*pca)//不为空，去删除
    {
        CA* cur = *pca;
        *pca = cur->p;
        free(cur);//free the place malloc;
    }
    else{
        perror("plist is NULL\n");
        return ;//已经为空
    }
}

void SL_deletefend(CA** pca)
{
    assert(pca);
    if(*pca)//不为空，去删除
    {
        if((*pca)->p)
        {
            CA* cur = *pca;
            while(cur->p->p)
            {
                cur = cur->p;
            }
            free(cur->p);//free the place malloc;
            cur->p = NULL;
        }
        else
        {
            free(*pca);
            *pca = NULL;
        }
    }
    else{
        perror("plist is NULL\n");
        return ;//已经为空
    }
}

CA* SLdfind(CA* phead,SLdata data)
{
    CA* cur = phead;
    while(cur)
    {
        if(cur->a== data)
        {
            return cur;
        }
        cur = cur->p;
    }
    printf("NO this DATA!\n");
    return NULL;
}

//某节点前插入
void SLindata(CA** phead,CA* pos,SLdata x)
{
    assert(phead);
    CA* cur = *phead;
    if(!(*phead))
    {
        *phead = Crea_node(x);
        return ;
    }
    if(!pos)
    {
        perror("pos is NULL\n");
        return;
    }
    if(*phead == pos)//solve teh probem if here only one value or it the head of the list insert
    {
        SL_firinsert(phead,x);
        return ;
    }
    while(cur && cur->p != pos)//if cur is NULL ,exit
    {
        cur = cur->p;
    }
    if(cur)
    {
        CA*newnode = Crea_node(x);
        newnode->p = pos;
        cur->p = newnode;
    }
    else
    {
        printf("NO pos int the list\n");
    }

}

void SLindata2(CA** phead,CA* pos,SLdata x)
{
    assert(phead);
    CA* cur = *phead;
    if(!(*phead))
    {
        *phead = Crea_node(x);
        return ;
    }
    if(!pos)
    {
        perror("pos is NULL\n");
        return;
    }
    while(cur && cur!=pos)
    {
        cur = cur->p;
    }
    if(cur)
    {
        CA*newnode = Crea_node(x);
        cur->p = newnode;
    }
    else
    {
        printf("NO pos int the list\n");
    }
}

void SLdele_aft(CA** phead,CA* pos)
{
    assert(phead);
    CA* cur = *phead;
    if(!(*phead))
    {
        printf("thee head is NULL!\n");
        return ;
    }
    while(cur && cur!=pos)
    {
        cur = cur->p;
    }
    if(!cur->p)
    {
        printf("the pos after the target is NULL.\n");
    }
    else
    {
        CA* t = pos->p;
        cur->p = cur->p->p;
        free(t);
    }
}


void SLdele_now(CA** phead,CA* pos)
{
    assert(phead);
    if(!pos)
    {
        perror("pos is NULL\n");
        return ;
    }
    CA* cur = *phead;
    if(!(*phead))
    {
        printf("thee head is NULL!\n");
        return ;
    }
    if(cur==pos)
    {
        *phead = cur->p;
        free(pos);
    }
    else
    {
        while(cur->p &&cur->p!=pos)
        {
            cur = cur->p;
        }
        if(cur->p)
        {
            cur->p = cur->p->p;
            free(pos);
        }
    }
}
